
#include "websocket.h"


static void wstcp_listen_cb(struct __IOCPFILE* hListen, struct __IOCPFILE* hClient);
static void wstcp_read_cb(HIOCPFILE hObject, void* pUserData);
static void wstcp_write_cb(HIOCPFILE hObject, void* pUserData);
static void wstcp_signal_call(HIOCPFILE hSocket, DWORD dwEvent, DWORD dwError);

IOWEBSOCKET* wsio_new(HIOCPBASE hIocp)
{
	IOWEBSOCKET* pWebSocket = NULL;

	if (NULL == hIocp)
	{
		return NULL;
	}

	pWebSocket = IOCP_MALLOC(sizeof(DNSIO));

	if (NULL == pWebSocket)
	{
		return NULL;
	}
	memset(pWebSocket, 0, sizeof(IOWEBSOCKET));

	pWebSocket->hIocp = hIocp;
	pWebSocket->hObject = fileio_create(pWebSocket->hIocp, NULL, IOTYPE_TCP);

	if (NULL == pWebSocket->hObject)
	{
		IOCP_FREE(pWebSocket);
		pWebSocket = NULL;
	}
	else
	{
		fileio_setcb(pWebSocket->hObject, wstcp_read_cb, wstcp_write_cb, wstcp_signal_call, pWebSocket);
	}

	return pWebSocket;
}

BOOL wsio_listen(IOWEBSOCKET* pWebSocket, SOCKADDR_IN* pBindAddr, PFN_WEBSOCKET_CONNECTPROC pfnAcceptProc)
{
	HIOCPFILE hListen = NULL;

	if (NULL != pWebSocket->hObject)
	{
		return FALSE;
	}

	hListen = fileio_create(pWebSocket->hIocp, NULL, IOTYPE_TCP);

	if (NULL == hListen)
	{
		return FALSE;
	}

	if (FALSE == fileio_listen(hListen, pBindAddr, wstcp_listen_cb))
	{
		fileio_close(hListen, 0);
		return FALSE;
	}

	pWebSocket->hObject = hListen;
	return TRUE;
}

BOOL wsio_accept(IOWEBSOCKET* pListen, IOWEBSOCKET* pAccept)
{
	BOOL bRet = fileio_accept(pListen->hObject, pAccept->hObject);
	if (TRUE == bRet)
	{

	}

	return bRet;
}

BOOL wsio_close(IOWEBSOCKET* pWebSocket, DWORD dwDelaySecond)
{
	HIOCPFILE hObject = pWebSocket->hObject;

	IOCP_FREE(pWebSocket);

	return fileio_close(hObject, dwDelaySecond);
}


void wstcp_listen_cb(struct __IOCPFILE* hListen, struct __IOCPFILE* hClient)
{

}

void wstcp_read_cb(HIOCPFILE hObject, void* pUserData)
{

}

void wstcp_write_cb(HIOCPFILE hObject, void* pUserData)
{

}

void wstcp_signal_call(HIOCPFILE hSocket, DWORD dwEvent, DWORD dwError)
{

}